package demogeneric;

/**
 * @Author 12629
 * @Description：
 */
//写一个泛型类，求一个泛型数组当中的最大值
    //将来传入的T 一定是实现了 Comparable 接口的
class Alg<T extends Comparable<T>> {
    public T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            //if(max < array[i]) {
            if(max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}
class Student implements Comparable<Student>{

    @Override
    public int compareTo(Student o) {
        return 0;
    }
}

class Alg2 {
    //泛型方法
    public<T extends Comparable<T>> T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class Alg3 {
    //泛型方法
    public static <T extends Comparable<T>> T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

public class Test2 {

    public static void main(String[] args) {
        Integer[] array = {1,2,3,14,5};
        Integer max = Alg3.<Integer>findMax(array);
        System.out.println(max);
    }

    public static void main2(String[] args) {
        Integer[] array = {1,2,3,14,5};
        Alg2 alg = new Alg2();
        Integer max = alg.<Integer>findMax(array);
        System.out.println(max);
    }

    public static void main1(String[] args) {
        Integer[] array = {1,2,3,14,5};
        Alg<Integer> alg = new Alg<>();
        Integer max = alg.findMax(array);
        System.out.println(max);

        /*Student[] students = new Student[3];
        Alg<Student> alg2 = new Alg<>();
        Student max2 = alg2.findMax(students);
        System.out.println(max2);*/

    }
}
